home *** CD-ROM | disk | FTP | other *** search
/ Personal Computer World 2009 February / PCWFEB09.iso / Software / Linux / Kubuntu 8.10 / kubuntu-8.10-desktop-i386.iso / casper / filesystem.squashfs / bin / hw-detect < prev    next >
Text File  |  2008-10-29  |  14KB  |  544 lines

  1. #!/bin/sh
  2.  
  3. set -e
  4. . /usr/share/debconf/confmodule
  5. #set -x
  6.  
  7. if [ -z "$1" ]; then
  8.     PROGRESSBAR=hw-detect/detect_progress_step
  9. else
  10.     PROGRESSBAR=$1
  11. fi
  12.  
  13. NEWLINE="
  14. "
  15. MISSING_MODULES_LIST=""
  16. SUBARCH="$(archdetect)"
  17.  
  18. finish_install=/usr/lib/finish-install.d/30hw-detect
  19.  
  20. LOAD_IDE=""
  21. if db_get hw-detect/load-ide && [ "$RET" = true ]; then
  22.     LOAD_IDE=1
  23. fi
  24.  
  25. if [ -x /sbin/depmod ]; then
  26.     depmod -a > /dev/null 2>&1 || true
  27. fi
  28.  
  29. log () {
  30.     logger -t hw-detect "$@"
  31. }
  32.  
  33. is_not_loaded() {
  34.     ! ((cut -d" " -f1 /proc/modules | grep -q "^$1\$") || \
  35.        (cut -d" " -f1 /proc/modules | sed -e 's/_/-/g' | grep -q "^$1\$"))
  36. }
  37.  
  38. is_available () {
  39.     [ "$(modprobe -l $1)" ] || return 1
  40. }
  41.  
  42. # Module as first parameter, description of device the second.
  43. missing_module () {
  44.     if ! in_list "$1" "$MISSING_MODULES_LIST"; then
  45.         if [ -n "$MISSING_MODULES_LIST" ]; then
  46.             MISSING_MODULES_LIST="$MISSING_MODULES_LIST, "
  47.         fi
  48.         MISSING_MODULES_LIST="$MISSING_MODULES_LIST$1 ($2)"
  49.     fi
  50. }
  51.  
  52. # The list can be delimited with spaces or spaces and commas.
  53. in_list() {
  54.     echo "$2" | grep -q "\(^\| \)$1\(,\| \|$\)"
  55. }
  56.  
  57. snapshot_devs() {
  58.     echo -n `grep : /proc/net/dev | sort | cut -d':' -f1`
  59. }
  60.  
  61. compare_devs() {
  62.     local olddevs="$1"
  63.     local devs="$2"
  64.     echo "${devs#$olddevs}" | sed -e 's/^ //'
  65. }
  66.  
  67. load_module() {
  68.     local module="$1"
  69.     local cardname="$2"
  70.     local devs=""
  71.     local olddevs=""
  72.     local newdev=""
  73.  
  74.     old=`cat /proc/sys/kernel/printk`
  75.     echo 0 > /proc/sys/kernel/printk
  76.  
  77.     devs="$(snapshot_devs)"
  78.     if log-output -t hw-detect modprobe -v "$module"; then
  79.         olddevs="$devs"
  80.         devs="$(snapshot_devs)"
  81.         newdevs="$(compare_devs "$olddevs" "$devs")"
  82.  
  83.         # Make sure space is used as a delimiter.
  84.         IFS_SAVE="$IFS"
  85.         IFS=" "
  86.         if [ -n "$newdevs" -a -n "$cardname" ]; then
  87.             mkdir -p /etc/network
  88.             for dev in $newdevs; do
  89.                 echo "${dev}:${cardname}" >> /etc/network/devnames
  90.             done
  91.         fi
  92.         IFS="$IFS_SAVE"
  93.     else   
  94.         log "Error loading '$module'"
  95.         if [ "$module" != floppy ] && [ "$module" != ide-floppy ] && \
  96.            [ "$module" != ide-cd ]; then
  97.             db_subst hw-detect/modprobe_error CMD_LINE_PARAM "modprobe -v $module"
  98.             db_input medium hw-detect/modprobe_error || [ $? -eq 30 ]
  99.             db_go
  100.         fi
  101.     fi
  102.  
  103.     echo $old > /proc/sys/kernel/printk
  104. }
  105.  
  106. # Some pci chipsets are needed or there can be DMA or other problems.
  107. get_ide_chipset_info() {
  108.     for ide_module in $(find /lib/modules/*/kernel/drivers/ide/pci/ -type f 2>/dev/null); do
  109.         if [ -e $ide_module ]; then
  110.             baseidemod=$(echo $ide_module | sed 's/\.ko$//; s/.*\///')
  111.             echo "$baseidemod:IDE chipset support"
  112.         fi
  113.     done
  114. }
  115.  
  116. # Return list of lines formatted "module:Description"
  117. get_detected_hw_info() {
  118.     if [ "${SUBARCH%%/*}" = powerpc ]; then
  119.         discover-mac-io
  120.         if [ "$SUBARCH" = powerpc/chrp_rs6k ] || \
  121.            [ "$SUBARCH" = powerpc/chrp_ibm ]; then
  122.             discover-ibm
  123.         fi
  124.     fi
  125.     if [ "${SUBARCH%%/*}" = sparc ]; then
  126.         discover-sbus
  127.     fi
  128.     if [ -d /proc/bus/usb ]; then
  129.         echo "usb-storage:USB storage"
  130.     fi
  131. }
  132.  
  133. # NewWorld PowerMacs don't want floppy or ide-floppy, and on some models
  134. # (e.g. G5s) the kernel hangs when loading the module.
  135. get_floppy_info() {
  136.     case $SUBARCH in
  137.         powerpc/powermac_newworld) ;;
  138.         *) echo "floppy:Linux Floppy" ;;
  139.     esac
  140. }
  141.  
  142. get_ide_floppy_info() {
  143.     case $SUBARCH in
  144.         powerpc/powermac_newworld) ;;
  145.         *) echo "ide-floppy:Linux IDE floppy" ;;
  146.     esac
  147. }
  148.  
  149. get_rtc_info() {
  150.     # On i386, this gets loaded by hotplug through isapnp, but that
  151.     # doesn't work on amd64.
  152.     case $SUBARCH in
  153.         amd64/*) register-module rtc ;;
  154.     esac
  155. }
  156.  
  157. # Manually load modules to enable things we can't detect.
  158. # XXX: This isn't the best way to do this; we should autodetect.
  159. # The order of these modules are important.
  160. get_manual_hw_info() {
  161.     if [ "$LOAD_IDE" ]; then
  162.         get_floppy_info
  163.         get_ide_chipset_info
  164.         echo "ide-generic:Linux IDE support"
  165.         get_ide_floppy_info
  166.         echo "ide-disk:Linux ATA DISK"
  167.         echo "ide-cd:Linux ATAPI CD-ROM"
  168.     fi
  169.     get_rtc_info
  170.  
  171.     # on some hppa systems, nic and scsi won't be found because they're
  172.     # not on a bus that udev understands ... 
  173.     if [ "`udpkg --print-architecture`" = hppa ]; then
  174.         echo "lasi_82596:LASI Ethernet"
  175.         register-module lasi_82596
  176.         echo "lasi700:LASI SCSI"
  177.         register-module -i lasi700
  178.         echo "zalon7xx:Zalon SCSI"
  179.         register-module -i zalon7xx
  180.     fi
  181.  
  182.     case $SUBARCH in
  183.         powerpc/ps3)
  184.             echo "ps3rom:PS3 internal CD-ROM drive"
  185.             echo "ps3disk:PS3 internal disk drive"
  186.             echo "ps3_gelic:PS3 Gigabit Ethernet"
  187.             register-module snd_ps3
  188.         ;;
  189.     esac
  190. }
  191.  
  192. # Should be greater than the number of kernel modules we can reasonably
  193. # expect it will ever need to load.
  194. MAX_STEPS=1000
  195. OTHER_STEPS=4
  196. # Use 1/10th of the progress bar for the non-module-load steps.
  197. OTHER_STEPSIZE=$(expr $MAX_STEPS / 10 / $OTHER_STEPS)
  198. db_progress START 0 $MAX_STEPS $PROGRESSBAR
  199.  
  200. db_progress INFO hw-detect/detect_progress_step
  201.  
  202. # TODO: Can possibly be removed if udev will load yenta_socket automatically
  203. # Load yenta_socket, if hardware is available, for Cardbus cards.
  204. if [ -d /sys/bus/pci/devices ] && \
  205.     grep -q 0x060700 /sys/bus/pci/devices/*/class 2>/dev/null && \
  206.     ! grep -q ^yenta_socket /proc/modules; then
  207.     db_subst hw-detect/load_progress_step CARDNAME "Cardbus bridge"
  208.     db_subst hw-detect/load_progress_step MODULE "yenta_socket"
  209.     db_progress INFO hw-detect/load_progress_step
  210.     
  211.     log "Detected Cardbus bridge, loading yenta_socket"
  212.     load_module yenta_socket
  213.     # Ugly hack, but what's the alternative?
  214.     sleep 3 || true
  215. fi
  216.  
  217. # If using real hotplug, re-run the rc scripts to pick up new modules.
  218. # TODO: this just loads modules itself, rather than handing back a list
  219. update-dev
  220.  
  221. ALL_HW_INFO=$(get_detected_hw_info; get_manual_hw_info)
  222. db_progress STEP $OTHER_STEPSIZE
  223.  
  224. # Remove modules that are already loaded or not available, and construct
  225. # the list for the question.
  226. LIST=""
  227. PROCESSED=""
  228. AVAIL_MODULES="$(find /lib/modules/$(uname -r)/ | sed 's!.*/!!' | cut -d . -f 1)"
  229. LOADED_MODULES="$(cut -d " " -f 1 /proc/modules) $(cut -d " " -f 1 /proc/modules | sed -e 's/_/-/g')"
  230. IFS_SAVE="$IFS"
  231. IFS="$NEWLINE"
  232. for device in $ALL_HW_INFO; do
  233.     module="${device%%:*}"
  234.     cardname="${device##*:}"
  235.     if [ "$module" != "ignore" -a "$module" != "" ] &&
  236.        ! in_list "$module" "$LOADED_MODULES" &&
  237.        ! in_list "$module" "$PROCESSED"
  238.     then
  239.         if [ -z "$cardname" ]; then
  240.             cardname="[Unknown]"
  241.         fi
  242.         
  243.         if in_list "$module" "$AVAIL_MODULES"; then
  244.             LIST="${LIST:+$LIST, }$module ($(echo "$cardname" | sed 's/,/ /g'))"
  245.             PROCESSED="$PROCESSED $module"
  246.         else
  247.             missing_module "$module" "$cardname"
  248.         fi
  249.     fi
  250. done
  251. IFS="$IFS_SAVE"
  252. db_progress STEP $OTHER_STEPSIZE
  253.  
  254. if [ "$LIST" ]; then
  255.     # Ask which modules to install.
  256.     db_subst hw-detect/select_modules list "$LIST"
  257.     db_set hw-detect/select_modules "$LIST"
  258.     db_input medium hw-detect/select_modules || true
  259.     db_go || exit 10 # back up
  260.     db_get hw-detect/select_modules
  261.     LIST="$RET"
  262. fi
  263.  
  264. list_to_lines() {
  265.     echo "$LIST" | sed 's/, /\n/g'
  266. }
  267.  
  268. # Work out amount to step per module load. expr rounds down, so 
  269. # it may not get quite to 100%, but will at least never exceed it.
  270. MODULE_STEPS=$(expr \( $MAX_STEPS - \( $OTHER_STEPS \* $OTHER_STEPSIZE \) \))
  271. if [ "$LIST" ]; then
  272.     MODULE_STEPSIZE=$(expr $MODULE_STEPS / $(list_to_lines | wc -l))
  273. fi
  274.  
  275. IFS="$NEWLINE"
  276.  
  277. for device in $(list_to_lines); do
  278.     module="${device%% *}"
  279.     cardname="`echo $device | cut -d'(' -f2 | sed 's/)$//'`"
  280.     # Restore IFS after extracting the fields.
  281.     IFS="$IFS_SAVE"
  282.  
  283.     if [ -z "$module" ] ; then module="[Unknown]" ; fi
  284.     if [ -z "$cardname" ] ; then cardname="[Unknown]" ; fi
  285.  
  286.     log "Detected module '$module' for '$cardname'"
  287.  
  288.     if is_not_loaded "$module"; then
  289.         db_subst hw-detect/load_progress_step CARDNAME "$cardname"
  290.         db_subst hw-detect/load_progress_step MODULE "$module"
  291.         db_progress INFO hw-detect/load_progress_step
  292.         if [ "$cardname" = "[Unknown]" ]; then
  293.             load_module "$module"
  294.         else
  295.             load_module "$module" "$cardname"
  296.         fi
  297.     fi
  298.  
  299.     db_progress STEP $MODULE_STEPSIZE
  300.     IFS="$NEWLINE"
  301. done
  302. IFS="$IFS_SAVE"
  303.  
  304. if [ -z "$LIST" ]; then
  305.     db_progress STEP $MODULE_STEPS
  306. fi
  307.  
  308. # Load ide-generic and check if that results in new block devices.
  309. # If so, make sure it is added to the initrd for the installed system.
  310. # Note: this may need to be done for more systems than just systems
  311. # that have an ISA bus, but that seems like a good start; it could also
  312. # be done unconditionally.
  313. if [ -z "$LOAD_IDE" ] && is_not_loaded ide-generic && \
  314.    [ -e /sys/bus/isa ] && is_available ide-generic; then
  315.     update-dev
  316.     blockdev_count=$(ls /sys/block | wc -w)
  317.  
  318.     log "ISA bus detected; loading module 'ide-generic'"
  319.     load_module ide-generic
  320.     update-dev
  321.     if [ $(ls /sys/block | wc -w) -gt $blockdev_count ]; then
  322.         register-module -i ide-generic
  323.     fi
  324. fi
  325.  
  326. if ! is_not_loaded ohci1394 || ! is_not_loaded firewire-ohci; then
  327.     # if firewire was found, try to enable firewire cd support
  328.     if is_not_loaded sbp2 && is_not_loaded firewire-sbp2 && \
  329.         is_available scsi_mod; then
  330.             sbp2module=
  331.         if is_available firewire-sbp2; then
  332.             sbp2module=firewire-sbp2
  333.         elif is_available sbp2; then
  334.             sbp2module=sbp2
  335.         fi
  336.         if [ -n "$sbp2module" ]; then
  337.             db_subst hw-detect/load_progress_step CARDNAME "FireWire CDROM support"
  338.             db_subst hw-detect/load_progress_step MODULE "$sbp2module"
  339.             db_progress INFO hw-detect/load_progress_step
  340.             load_module "$sbp2module"
  341.             register-module "$sbp2module"
  342.         else
  343.             missing_module firewire-sbp2 "FireWire CDROM"
  344.         fi
  345.     fi
  346.     db_progress STEP $OTHER_STEPSIZE
  347. fi
  348.  
  349. # Always load the printer driver on i386 and amd64; it's hard to autodetect.
  350. case $SUBARCH in
  351.     i386/*|amd64/*)
  352.         register-module lp
  353.         ;;
  354. esac
  355.  
  356. apply_pcmcia_resource_opts() {
  357.     local config_opts=/etc/pcmcia/config.opts
  358.     
  359.     # Idempotency
  360.     if ! [ -f ${config_opts}.orig ]; then
  361.         cp $config_opts ${config_opts}.orig
  362.     fi
  363.     cp ${config_opts}.orig $config_opts
  364.  
  365.     local mode=""
  366.     local rmode=""
  367.     local type=""
  368.     local value=""
  369.     while [ -n "$1" ] && [ -n "$2" ] && [ -n "$3" ]; do
  370.         if [ "$1" = exclude ]; then
  371.             mode=exclude
  372.             rmode=include
  373.             shift
  374.         elif [ "$1" = include ]; then
  375.             mode=include
  376.             rmode=exclude
  377.             shift
  378.         fi
  379.         type="$1"
  380.         shift
  381.         value="$1"
  382.         shift
  383.         
  384.         if grep -q "^$rmode $type $value\$" $config_opts; then
  385.             sed "s/^$rmode $type $value\$/$mode $type $value/" \
  386.                 $config_opts >${config_opts}.new
  387.             mv ${config_opts}.new $config_opts
  388.         else
  389.             echo "$mode $type $value" >>$config_opts
  390.         fi
  391.     done
  392. }
  393.  
  394. # get pcmcia running if possible
  395. PCMCIA_INIT=
  396. if [ -x /etc/init.d/pcmciautils ]; then
  397.     PCMCIA_INIT=/etc/init.d/pcmciautils
  398. fi
  399. if [ "$PCMCIA_INIT" ]; then
  400.     if is_not_loaded pcmcia_core; then
  401.         db_input medium hw-detect/start_pcmcia || true
  402.         db_input low hw-detect/pcmcia_resources || true
  403.         db_go || true
  404.         if db_get hw-detect/pcmcia_resources && [ "$RET" ]; then
  405.             apply_pcmcia_resource_opts $RET
  406.         fi
  407.     fi
  408.     if db_go && db_get hw-detect/start_pcmcia && [ "$RET" = true ]; then
  409.         db_progress INFO hw-detect/pcmcia_step
  410.         $PCMCIA_INIT start 2>&1 | log
  411.         db_progress STEP $OTHER_STEPSIZE
  412.     fi
  413. fi
  414.  
  415. have_pcmcia=0
  416. if ls /sys/class/pcmcia_socket/* >/dev/null 2>&1; then
  417.     if db_get hw-detect/start_pcmcia && [ "$RET" = false ]; then
  418.         have_pcmcia=0
  419.     else
  420.         have_pcmcia=1
  421.     fi
  422. fi
  423.  
  424. # find Cardbus network cards
  425. cardbus_check_netdev()
  426. {
  427.     local socket="$1"
  428.     local netdev="$2"
  429.     if [ -L $netdev/device ] && \
  430.         [ -d $socket/device/$(basename $(readlink $netdev/device)) ]; then
  431.         echo $(basename $netdev) >> /etc/network/devhotplug
  432.     fi
  433. }
  434. if ls /sys/class/pcmcia_socket/* >/dev/null 2>&1; then
  435.     for socket in /sys/class/pcmcia_socket/*; do
  436.         for netdev in /sys/class/net/*; do
  437.             cardbus_check_netdev $socket $netdev
  438.         done
  439.     done
  440. fi
  441.  
  442. # Try to do this only once..
  443. if [ "$have_pcmcia" -eq 1 ] && \
  444.    ! grep -q pcmciautils /var/lib/apt-install/queue 2>/dev/null; then
  445.     log "Detected PCMCIA, installing pcmciautils."
  446.     apt-install pcmciautils || true
  447.  
  448.     if db_get hw-detect/pcmcia_resources && [ -n "$RET" ]; then
  449.         echo "mkdir /target/etc/pcmcia 2>/dev/null || true" \
  450.             >>$finish_install
  451.         echo "cp /etc/pcmcia/config.opts /target/etc/pcmcia/config.opts" \
  452.             >>$finish_install
  453.     fi
  454. fi
  455.  
  456. # Install udev into target
  457. apt-install udev || true
  458.  
  459. # TODO: should this really be conditional on hotplug support?
  460. if [ -f /proc/sys/kernel/hotplug ]; then
  461.     apt-install usbutils || true
  462. fi
  463.  
  464. # Install acpi
  465. if [ -d /proc/acpi ]; then
  466.     apt-install acpi || true
  467.     apt-install acpid || true
  468.     apt-install acpi-support-base || true
  469. fi
  470.  
  471. # If hardware has support for pmu, install pbbuttonsd
  472. if [ -d /sys/class/misc/pmu/ ]; then
  473.     apt-install pbbuttonsd || true
  474. fi
  475.  
  476. # Install mouseemu on systems likely to have single-button mice
  477. case $SUBARCH in
  478.     i386/mac|amd64/mac)
  479.         apt-install mouseemu || true
  480.     ;;
  481.     powerpc/powermac_*)
  482.         # mouseemu causes an oops somewhere in the input layer on
  483.         # powerpc64 at the moment, so don't install it.
  484.         if [ ! -d /proc/ppc64 ]; then
  485.             apt-install mouseemu || true
  486.         fi
  487.     ;;
  488. esac
  489.  
  490. # Install eject?
  491. if [ -n "$(list-devices cd; list-devices maybe-usb-floppy)" ]; then
  492.     apt-install eject || true
  493. fi
  494.  
  495. # Install optimised libc based on CPU type
  496. case "$(udpkg --print-architecture)" in
  497.     i386)
  498.     case "$(grep '^cpu family' /proc/cpuinfo | cut -d: -f2)" in
  499.         " 6"|" 15")
  500.         # intel 686 or Amd k6.
  501.         apt-install libc6-i686 || true
  502.                 ;;
  503.     esac
  504.     ;;
  505.     sparc)
  506.     if grep -q '^type.*: sun4u' /proc/cpuinfo ; then
  507.         # sparc v9 or v9b
  508.         if grep -q '^cpu.*: .*UltraSparc III' /proc/cpuinfo; then
  509.             apt-install libc6-sparcv9b || true
  510.         else
  511.             apt-install libc6-sparcv9 || true
  512.         fi
  513.     fi
  514.     ;;
  515. esac
  516.  
  517. # Install PS3 utilities
  518. case $SUBARCH in
  519.     powerpc/ps3)
  520.         apt-install ps3pf-utils || true
  521.         ;;
  522. esac
  523.  
  524. # Install Cell utilities
  525. case $SUBARCH in
  526.     powerpc/ps3|powerpc/cell)
  527.         apt-install elfspe2 || true
  528.         ;;
  529. esac
  530.  
  531. db_progress SET $MAX_STEPS
  532. db_progress STOP
  533.  
  534. if [ -n "$MISSING_MODULES_LIST" ]; then
  535.     log "Missing modules '$MISSING_MODULES_LIST"
  536. fi
  537.  
  538. sysfs-update-devnames
  539.  
  540. # Let userspace /dev tools rescan the devices
  541. update-dev
  542.  
  543. exit 0
  544.